package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO<Item> pageList(@RequestParam Integer page, @RequestParam Integer size){
        log.info("分页查询:{},{}",page,size);
       Page<Item> result =  itemService.page(new Page<>(page,size));
            return new PageDTO<>(result.getTotal(), result.getRecords());
    }
    @GetMapping("{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }
    @PostMapping
    public void insert(@RequestBody Item item){
        itemService.save(item);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(1);
    }
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable Integer status){
        itemService.updateStatus(id,status);
    }

    @PutMapping
    public void updateInfo(@RequestBody Item item){
        itemService.updateById(item);
        item.setUpdateTime(new Date());
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable Long id){
        itemService.removeById(id);
    }

    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num){
        itemService.deductStock(itemId, num);
    }
}
